package com.auxgroup.hr.front.site.co.hireperson;

import javax.annotation.Resource;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.auxgroup.hr.front.commons.utils.IDCardUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.dao.domain.PsCHirePerTbl;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.hireperson.HirePersonService;
import com.auxgroup.hr.front.site.co.BaseCO;

/**
 * 入职人员个人信息CO
 *
 * @author lvliuhao
 * @version 创建时间：180526
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/hirePerson")
public class HirePersonCO extends BaseCO {

    private static final Logger logger = LoggerFactory.getLogger(HirePersonCO.class);

    @Autowired
    public HirePersonService hirePersonService;

    @Resource(name = "dozerMapper")
    protected IObjectMapper objectMapper;

    /**
     * 保存入职人员基本信息
     *
     * @param hirePerson
     * @return
     */
    @PostMapping(value = "/save")
    public BaseResponseVO save(PsCHirePerTbl hirePerson) {
        logger.info("saveHirePerson param= {}", hirePerson);
        if (StringUtils.isBlank(hirePerson.getRsHireId())) {
            return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
        }
        PsCHirePerTbl nowPer = hirePersonService.updateBaseInfo(hirePerson);
        return buildRespone(nowPer.getRsHireId() != null);
    }

    /**
     * 保存入职人员联系信息
     *
     * @param hirePerson
     * @return
     */
    @PostMapping(value = "/saveConcatInfo")
    public BaseResponseVO saveConcatInfo(PsCHirePerTbl hirePerson) {
        logger.info("saveConcatInfo param= {}", hirePerson);
        if (StringUtils.isBlank(hirePerson.getRsHireId())) {
            return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
        }
        PsCHirePerTbl nowPer = hirePersonService.updateContactInfo(hirePerson);
        return buildRespone(nowPer.getRsHireId() != null);
    }

    /**
     * 获取入职人员基本信息
     *
     * @param rsHireId
     * @return
     */
    @GetMapping(value = "/getOne")
    public BaseResponseVO getOne(String rsHireId) {
        logger.info("getHirePerson param= {}", rsHireId);
        if (StringUtils.isBlank(rsHireId)) {
            return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(), "入职人员ID不能为空");
        }
        PsCHirePerTbl hirePerson = hirePersonService.gethirePer(rsHireId);
        if (hirePerson == null) {
            return buildRespone("");
        }
//		if("C".equals(hirePerson.getRsHirStatus())||"U".equals(hirePerson.getRsHirStatus())){
//			return buildRespone("");
//		}
        return buildRespone(hirePerson);
    }

    /**
     * 新员工登陆
     *
     * @param hirePerson
     * @return
     */
    @PostMapping(value = "/entry")
    public BaseResponseVO entry(PsCHirePerTbl hirePerson) {
        logger.info("entry param= {}", JSON.toJSONString(hirePerson));
        if (StringUtils.isBlank(hirePerson.getNationalId()) || StringUtils.isBlank(hirePerson.getName())) {
            return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(), "请填写身份证号和姓名");
        }
        if (!IDCardUtil.isValidatedAllIdcard(hirePerson.getNationalId())) {
            return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(), "请输入正确的身份证号");
        }
        PsCHirePerTbl result = hirePersonService.getByNationalId(hirePerson.getNationalId());
        if (result == null) {
            //一线员工新建用户
//			result = hirePersonService.initNewHirePerson(hirePerson.getNationalId(), hirePerson.getName());
//			return buildRespone(result);
            return buildRespone(BaseRetCode.CODE_ERROR_PERSONDATA_NOT_EXIST.getRetCode(), "未匹配到提前登记身份信息，请联系对应HR进行身份证核对。");
        }
        if (!PsCHirePerTbl.PLAT_HOUQING.equals(result.getPlatLx())) {
            return buildRespone(BaseRetCode.CODE_ERROR_AUTH_UNAUTH.getRetCode(), "未匹配到提前登记身份信息，请联系对应HR进行身份证核对。");
        }
        if (StringUtils.isNotBlank(result.getName()) && !StringUtils.equals(result.getName(), hirePerson.getName())) {
            return buildRespone(BaseRetCode.CODE_ERROR_INPUT_NOT_RIGHT.getRetCode(), "身份证或用户信息错误");
        }
//		if("C".equals(hirePerson.getRsHirStatus())||"U".equals(hirePerson.getRsHirStatus())){
//			return buildRespone("");
//		}
        return buildRespone(result);
    }
}
